package org.leetcode.easy.leetcode169;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int majorityElement(int[] nums) {

        if (nums.length==1)
        {
            return nums[0];
        }

        HashMap<Integer,Integer> hashMap = new HashMap<>();

        int max =0;

        int res =0;

        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i])) {
                hashMap.put(nums[i],hashMap.get(nums[i])+1);
               if (max<hashMap.get(nums[i]))
               {
                   max=hashMap.get(nums[i]);
                    res = nums[i];
               }
            }else {
                hashMap.put(nums[i],1);
            }
        }

        Set<Map.Entry<Integer, Integer>> entries = hashMap.entrySet();



        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        int [] test= {1};

        int i = solution.majorityElement(test);
        System.out.println(i);

    }
}
